#include "head.h"

int server_mkdir(int net_fd,self_defined_pro_t *mes){

    char path[60]={0};
    char result[60]={0};
    
    char *username=strtok(mes->message,"=");
    username=strtok(NULL,"&");
    char *runtime_path=strtok(NULL,"=");
    runtime_path=strtok(NULL,"&");
    char *dir=strtok(NULL,"=");
    dir=strtok(NULL,"&");
    strcpy(path,load_config().file);
    strcat(path,"/");
    strcat(path,username);
    strcat(path,runtime_path);
    DIR *dirp=opendir(path);
    struct dirent *pdirent;

    self_defined_pro_t *send_mes=(self_defined_pro_t *)calloc(1,sizeof(self_defined_pro_t));
    while((pdirent=readdir(dirp))!=NULL){
        struct stat stat_buf;
        stat(pdirent->d_name,&stat_buf);
        if((stat_buf.st_mode& S_IFMT)==S_IFDIR){
            if(strcmp(pdirent->d_name,dir)==0){
                send_mes->cmd=400;
                send_mes->para_num=1;
                strcpy(result,"result=目录已存在");
                strcpy(send_mes->message,result);
                send_mes->message_len=strlen(send_mes->message);
                send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);
                return -1;
            }
        }                                                                             
    }
    if(strlen(runtime_path) != 1){
        strcat(path,"/");
    }
    strcat(path,dir);
    mkdir(path,0777);
    if(mes->cmd==6){
        strcpy(result,"dir=");
        strcat(result,dir);
        strcat(result,"创建成功");
        send_mes->cmd=200;
        send_mes->para_num=1;
        strcpy(send_mes->message,result);
        send_mes->message_len=strlen(send_mes->message);
        send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);
    }
     return 0;
 }


int server_mkdir1(int net_fd,self_defined_pro_t *mes){

    char path[60]={0};
    char base_path[60]={0};
    char result[60]={0};
    getcwd(base_path,sizeof(base_path));
    strcpy(path,base_path);
    strcat(path,"/");
    strcat(path,mes->message);
    DIR *dirp=opendir(base_path);
    struct dirent *pdirent;

    // TODO:接收用户名, 当前路径, 创建的文件夹,  拼接-> 创建

    self_defined_pro_t *send_mes=(self_defined_pro_t *)calloc(1,sizeof(self_defined_pro_t));
    while((pdirent=readdir(dirp))!=NULL){
        struct stat stat_buf;
        stat(pdirent->d_name,&stat_buf);
        if((stat_buf.st_mode& S_IFMT)==S_IFDIR){
            if(strcmp(pdirent->d_name,mes->message)==0){
                send_mes->cmd=-1;
                send_mes->para_num=2;
                strcpy(result,"login_status=1");
                strcat(result,"&");
                strcat(result,"result=目录已存在");
                strcpy(send_mes->message,result);
                send_mes->message_len=strlen(send_mes->message);
                send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);
                return -1;
            }
        }                                                                             
    }
    printf("server_mkdir: path: %s \n", path);
    mkdir(path,0777);
    if(mes->cmd==6){
        strcpy(result,"dir=");
        strcat(result,mes->message);
        strcat(result,"创建成功");
        send_mes->cmd=-2;
        send_mes->para_num=1;
        strcpy(send_mes->message,result);
        send_mes->message_len=strlen(send_mes->message);
        send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);
    }
     return 0;
 }

